﻿@using RiderShop.Mobile.Common.Component
@using RiderShop.Mobile.Modle.Purchase
@section title
{
    @if (Request.QueryString["cardType"] == "xx")
    {
        @Html.Raw("鲜享卡支付");
    }
    else
    {
        @Html.Raw("银信商通卡支付");
    }
}
@section pagetitle
{
    @if (Request.QueryString["cardType"] == "xx")
    {
        @Html.Raw("鲜享卡支付");
    }
    else
    {
        @Html.Raw("银信商通卡支付");
    }
}

@section header
{
    <link rel="stylesheet" href="~/Themes/RongJi/Style/base/css/sign.css?v=1">
    <style>
        .innerwrapper {
            display: none;
        }

        .body {
            margin-top: 10%;
        }

        .amount {
            margin-top: 20px;
            text-align: center;
            font-size: 16px;
        }

        .btn-green {
            background: #79c9a5;
        }

        input#cardNumber {
            ime-mode: disabled;
        }

        input#cardNumber {
            ime-mode: disabled;
        }
    </style>
}
@model RiderShop.Mobile.Modle.Purchase.PaymentModel

@if (Request.QueryString["cardType"] == "xx")
{
    <div class="top"><img src="~/Themes/RongJi/Style/base/imgs/card.jpg" alt=""></div>
}
else
{
    <div class="top"><img src="~/Themes/RongJi/Style/base/imgs/yinxincard.jpg" alt=""></div>

}
@{
    decimal discount = 0M;
    if (Request.QueryString["cardType"] == "xx")
    {
        discount = Model.GetOrderPaymentDiscount(PaymentPlatformType.XianXiangCard);
    }
}

<div class="amount">
    订单金额：<span class="orange">￥@Model.OrderBasicInfo.OrderPayAmount</span>
    <span id="discountSpan" @(discount > 0 ? "" : "style=display:none")>
        <br />立减<span class="orange" id="discount">￥@discount</span> 应付<span class="orange" id="orderPayAmount">￥@(Model.OrderBasicInfo.OrderPayAmount - discount)</span>
    </span>
    <br /><span>联名卡输入卡号查看更多优惠</span>
</div>

<form action="~/Purchase/YinxinPay" method="POST" id="yixinPayForm">
    <div class="body">
        <input type="hidden" id="cardType" name="cardType" value="@Request.QueryString["cardType"]" />
        <input type="hidden" id="orderNumber" name="orderNumber" value="@Model.OrderBasicInfo.OrderNumber" />

        @if (Request.QueryString["cardType"] == "xx")
        {
            <input type="tel" value="2980021500" class="form-control" id="cardNumber" name="cardNumber" maxlength="30" placeholder="请输入卡号">
        }
        else
        {
            <input type="tel" value="29800200000" class="form-control" id="cardNumber" name="cardNumber" maxlength="30" placeholder="请输入卡号">
        }

        <input type="password" class="form-control" id="password" name="password" placeholder="请输入密码" maxlength="20" autocomplete="off">

        @if (!string.IsNullOrEmpty(Model.PaymentResult))
        {
            <span class="err_msg">@Model.PaymentResult</span>
        }
        else
        {
            <span class="err_msg" id="msgError"></span>
        }
    </div>
    <div class="bottom">
        <div class="btn-group btn-group-justified btn-wrapper" role="group" aria-label="...">
            <div class="btn-group" role="group">
                <input type="button" id="btnyixinQuery" class="btn" value="余额">
            </div>
            <div class="btn-group" role="group">
                <input type="button" id="btnyixinPay" class="btn btn-green" value="支付">
            </div>
        </div>
    </div>
</form>


@section footer
{
    <script type="text/javascript">
        $(function () {

            function getDiscount(cardNumber) {
                if (cardNumber.length === 19) {

                    var data = {
                        orderNumber: $('#orderNumber').val(),
                        cardNumber: cardNumber
                    }
                    KevinShop.Common.Ajax.Post('/Purchase/YinxinDiscount', JSON.stringify(data), function (response) {
                        if (response && response.discount > 0) {
                            $('#discount').text('￥' + response.discount);
                            $('#orderPayAmount').text('￥' + response.orderPayAmount);
                            $('#discountSpan').show();
                        } else {
                            $('#discountSpan').hide();
                        }
                    });
                }
            }
            var cookieName = '@(Request.QueryString["cardType"] + "CardNumber")';


            var cardNumber = $.cookie(cookieName);
            if (cardNumber) {
                $("#cardNumber").val(cardNumber);
                getDiscount(cardNumber);
            }

            $('#cardNumber').bind('input propertychange', function () {
                var text = $(this).val();

                getDiscount(text);
            });


            $("#btnyixinPay").click(function () {
                if ($("#cardNumber").val() == "") {
                    KevinShop.MessageBox.Alert("请输入用户名!");
                    return false;
                } if ($("#password").val() == "") {
                    KevinShop.MessageBox.Alert("请输入密码!");
                    return false;
                }

                var cfm = confirm('确认支付' + $('#orderPayAmount').val() + '？');
                if (!cfm) {
                    return;
                }

                $.cookie(cookieName, $("#cardNumber").val());

                var postdata = $("#yixinPayForm").serialize();
                //document.forms["yixinPayForm"].submit();
                var loadMark = KevinShop.Common.Loading;
                $.ajax({
                    type: "post",
                    url: $("#yixinPayForm").attr('Action'),
                    data: postdata,
                    contentType: "application/x-www-form-urlencoded; charset=utf-8",
                    beforeSend: function (XMLHttpRequest) {
                        loadMark.show();
                        $("#msgError").html("正在支付，请稍等。");
                    },
                    success: function (data) {
                        if (data === "1") {
                            window.location.href = "/Purchase/PaymentCompleted?orderNumber=" + $("#orderNumber").val();
                        } else if (data === "0") {
                            window.location.href = "/myaccount/OrderList?orderStatus=1";
                        } else {
                            $("#msgError").html(data);
                        }
                    },
                    complete: function (XMLHttpRequest, textStatus) {
                        //HideLoading();
                        loadMark.hide();
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        //请求出错处理
                        $("#msgError").html("服务器异常，请刷新页面重试！");
                        loadMark.hide();
                    }
                });
            });


            $("#btnyixinQuery").click(function () {
                if ($("#cardNumber").val() == "") {
                    KevinShop.MessageBox.Alert("请输入用户名!");
                    return false;
                } if ($("#password").val() == "") {
                    KevinShop.MessageBox.Alert("请输入密码!");
                    return false;
                }

                $.cookie(cookieName, $("#cardNumber").val());

                var postdata = $("#yixinPayForm").serialize();
                //document.forms["yixinPayForm"].submit();
                var loadMark = KevinShop.Common.Loading;
                $.ajax({
                    type: "post",
                    url: '/Purchase/YinxinBalance',
                    data: postdata,
                    contentType: "application/x-www-form-urlencoded; charset=utf-8",
                    beforeSend: function (XMLHttpRequest) {
                        loadMark.show();
                        $("#msgError").html("正在查询，请稍等。");
                    },
                    success: function (data) {
                        $("#msgError").html(data);
                    },
                    complete: function (XMLHttpRequest, textStatus) {
                        //HideLoading();
                        loadMark.hide();
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        //请求出错处理
                        $("#msgError").html("服务器异常，请刷新页面重试！");
                        loadMark.hide();
                    }
                });
            });


        });
    </script>
}
